<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>信息维护</title>
		<!--引入wangEditor.css-->
		<link rel="stylesheet" href="../dist/css/index.css" />
		<link rel="stylesheet" type="text/css" href="../dist/css/wangEditor.min.css">
		<link rel="stylesheet" href="../dist/css/pnotify.custom.min.css" />
		<script type="text/javascript" src="../dist/js/lib/jquery-2.2.1.js"></script>
		<script type="text/javascript" src="../dist/js/pnotify.custom.min.js"></script>
	</head>
	<style>
	</style>

	<body>
		<div id='top_bg'>
			<nav id="nav">
				<img src="../dist/img/shake_back_normal.png" class="back" onclick="goback()" /> 信息维护
				<p class="subbtn" onclick="getData()">发布</p>
			</nav>
		</div>
		<!--用父容器来控制宽度-->
		<div class="contenttop">
			<p><label>标题:</label><input type="text" name="name_tit_type" id="text_tit" class="name_text" value="" placeholder="请输入标题不超过15字..." maxlength="15"></p>
			<p><label>摘要:</label><input type="text" name="name_zy" id="text_zy" class="name_text" value="" placeholder="请输入文章摘要不超过120字..." maxlength="120"></p>
			<p><label>标签:</label><input type="text" name="name_bq" id="text_bq" class="label_bq" value="" placeholder="标签不超过6字..." maxlength="6">
				<button type="button" id="su" name="con_type" class="add_bq" onclick="addBq()">添加</button>
			</p>
			<div class="box_bq_add">
				<ul id="taglist">
				</ul>
			</div>
			<div>
				<div class="" style="float: left; margin-left: 5px">
					 <input type="checkbox" id="carousel">是否轮播</input><br/><br/>
					 <span>视频</span> <input type="radio" id="video" name="vicpic" class="vp_1" value="视频" />
					 <span>图文</span> <input type="radio" id="video" name="vicpic" class="vp_2" value="图文" checked="true" />
					<br/><br/>
					<span>类型:</span>
					<input list="datalist" name="name_con_type" id="text_con_type" class="name_text_type" />
					<datalist id="datalist">
						        <option value="美文">美文</option>
								<option value="美女">美女</option>
								<option value="美食">美食</option>
								<option value="艺术">艺术</option>
								<option value="人文">人文</option>
								<option value="新闻">新闻</option>
								<option value="时政">时政</option>
								<option value="科技">科技</option>
								<option value="生活">生活</option>
								<option value="军事">军事</option>
								<option value="旅游">旅游</option>
								<option value="环境">环境</option>
								<option value="自然">自然</option>
							</datalist>
							<br/><br/>
				</div>
				<div class="" style="float: right; margin-right: 1%;text-align: center;">
					<div style="float: left; min-height:50px;line-height: 5rem;">	封面图片:</div>
					<div type="button" class="btn_sel" id="up_img">
						<input type="hidden" id="img_url" />
						<a href="javascript:showDialog()" class="current">
							<div class="tjtp"></div>
						</a>
					</div>
				</div>
			</div>
			
		</div>

		<div id="content">
			<!--用当前元素来控制高度  请输入内容... -->
			<div id="div1" style="height: 500px; min-height: 200px;"></div>
		</div>
	</body>

	<script type="text/javascript">
	function goback(){
		window.history.back(1);
		window.AndroidWebView.showInfoFromJs('返回')
	}
		window.onscroll = function() {
			var t = document.documentElement.scrollTop || document.body.scrollTop;
			var top_div = document.getElementById("nav");
			if(t >= 200) {
				top_div.style.background = "rgba(51,103,214,0.9)";
			} else {
				top_div.style.background = "rgba(51,103,214,0.5)";
			}
		}
		//  获取元素对象  
		function g(id) {
			return document.getElementById(id);
		}
		//  自动居中元素（el = Element）
		function autoCenter(el) {
			var bodyW = document.documentElement.clientWidth;
			var bodyH = document.documentElement.clientHeight;
			var elW = el.offsetWidth;
			var elH = el.offsetHeight;
			el.style.left = (bodyW - elW) / 2 + 'px';
			el.style.top = (bodyH - elH) / 2 + 'px';
		}
		//  自动扩展元素到全部显示区域
		function fillToBody(el) {
			el.style.width = document.documentElement.clientWidth + 'px';
			el.style.height = document.documentElement.clientHeight + 'px';
		}
		//  重新调整对话框的位置和遮罩，并且展现
		function showDialog() {
			loginDialog()
		}
		
		
		 var  notice=null;
		    function loginDialog(){
				<!--<input type="file" id="head_upload"  accept="image/jpg,image/png" name="myFileName" value=""  />-->
					if(notice!=null){
						 notice.open();
					}else{
						var html="<form id='uploadPicForms' accept-charset='utf-8' method='post' enctype='multipart/form-data'>"+
			    		"<div id='mydialog' class='ui-dialog-content-select_pic'>"+
			    	        "<input type='text' id='head_upload_txt_ids' name='' class='ui-dialog-content-select_pic_name' />"+
							"<input type='button' class='ui-dialog-content-select_pic_look' value='浏览...' /> "+
							"<input type='file' id='head_upload_ids' name='myFileName' class='ui-dialog-content_pic_sel' size='28' accept='image/jpg,image/png'  />"+
							"<div class='line'></div>"+
							"<div class='ui-dialog-content-bot'>"+
							   "<div class='ui-dialog-content-canel'>"+
							     "<a class='ui-dialog-content-canel' href='javascript:notice.remove();'>取消</a>"+
							    "</div>"+
							    "<div class='ui-dialog-content-uplo'>"+
							      "<input type='button' value='上传' class='ui-dialog-content-uplo' onclick='upPic()'>"+
							    "</div>"+
							   "</div>"+
							"</div></form>";
							//onchange='document.getElementById('head_upload_txt_id').value=this.value'
							if(notice==null){
								  notice= new PNotify({
									    title: '上传封面图',
									    text: html,
									    type: 'info',
									     buttons: {
									        closer: false,
									        sticker: false
									    }, 
									    mobile:{
									    	styling:true,
									    	swipe_dismiss:false
									    },
									    delay:100000,
									    remove:false
									  });
							}
					}	
		    	
		    	  $('#head_upload_ids').change(function(){
		    		  console.log(notice);
		    		  $('#head_upload_txt_ids').val($('#head_upload_ids').val())
		    	  });
				//notice.get().click(function() {
				//});
				
		    }
	</script>

	<!--引入jquery和wangEditor.js-->
	<!--注意：javascript必须放在body最后，否则可能会出现问题-->
	<script type="text/javascript" src="../dist/js/lib/jquery-1.10.2.min.js"></script>
	<script type="text/javascript" src="../dist/js/wangEditor.js"></script>
	<!--这里引用jquery和wangEditor.js-->
	<script type="text/javascript">
		//阻止输出log
		// wangEditor.config.printLog = false;

		var editor = new wangEditor('div1');
		// 上传图片（举例）
		editor.config.uploadImgUrl = getContextPath() + "/WangEditorImgUpload";

		// 配置自定义参数（举例）
		editor.config.uploadParams = {
			token: 'abcdefg',
			user: 'wangfupeng1988'
		};

		// 设置 headers（举例）
		editor.config.uploadHeaders = {
			'Accept': 'text/x-json'
		};

		editor.config.uploadImgFileName = 'myFileName'

		// 表情显示项
		editor.config.emotionsShow = 'icon'; //icon/value
		editor.config.emotions = {
			'default': {
				title: '默认',
				data: '../dist/js/emotions.data'
			},
			'weibo': {
				title: '微博表情',
				data: [{
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_thumb.gif',
					value: '[草泥马]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/60/horse2_thumb.gif',
					value: '[神马]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/bc/fuyun_thumb.gif',
					value: '[浮云]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c9/geili_thumb.gif',
					value: '[给力]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/f2/wg_thumb.gif',
					value: '[围观]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/70/vw_thumb.gif',
					value: '[威武]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/6e/panda_thumb.gif',
					value: '[熊猫]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/81/rabbit_thumb.gif',
					value: '[兔子]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/bc/otm_thumb.gif',
					value: '[奥特曼]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/15/j_thumb.gif',
					value: '[囧]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/89/hufen_thumb.gif',
					value: '[互粉]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c4/liwu_thumb.gif',
					value: '[礼物]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/ac/smilea_thumb.gif',
					value: '[呵呵]'
				}, {
					icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/0b/tootha_thumb.gif',
					value: '[哈哈]'
				}]
			}
		};
		// onchange 事件
		editor.onchange = function() {
			//console.log(this.$txt.html());
		};
		// 隐藏掉插入网络图片功能。该配置，只有在你正确配置了图片上传功能之后才可用。
		editor.config.hideLinkImg = false;
		editor.create();
		$('#btn1').click(function() {
			// 获取编辑器区域完整html代码
			var html = editor.$txt.html();
			// 获取编辑器纯文本内容
			// var text = editor.$txt.text();
			// 获取格式化后的纯文本
			// var formatText = editor.$txt.formatText();
		});

		/**
		 * 访问地址
		 */
		function getContextPath() {
			// 获取当前网址，如：http://localhost:8080/ssm/index.jsp

			var currentPath = window.document.location.href;
			// 获取主机地址之后的目录，如： /ssm/index.jsp
			var pathName = window.document.location.pathname;
			var pos = currentPath.indexOf(pathName);
			// 获取主机地址，如： http://localhost:8080
			var localhostPath = currentPath.substring(0, pos);
			// 获取带"/"的项目名，如：/ssm
			var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
			// 	    alert("currentPath:" + currentPath + "\n"
			// 	            + "pathName:" + pathName + "\n"
			// 	            + "localhostPath:" + localhostPath + "\n"
			// 	            + "projectName:" + projectName + "\n"
			// 	            + "contextPath:"+ localhostPath + projectName);
			return(localhostPath + projectName);
		}
	</script>

	<script type="text/javascript">
		//添加标签
		function addBq() {
			var li = document.createElement("li");
			var add_bq_nr = document.getElementById('text_bq').value;
			if(add_bq_nr == "" || add_bq_nr.length == 0) {
				msg('请填写上传标签！', 'info')
				return;
			}
			li.innerText = add_bq_nr;
			document.getElementById('text_bq').value = "";
			document.getElementById('taglist').appendChild(li)
			li.onclick = function() {
				this.remove()
			};
			//			alert(document.getElementById('text_bq').value)
		}

		// 上传图片
		function upPic() {
			//			

			var imgPath = document.getElementById('head_upload_ids').value;
			if(imgPath == "" || imgPath.length == 0) {
				msg('请选择图片...', 'info')
				return;
			}

			var formData = new FormData($("#uploadPicForms")[0]);
			$.ajax({
				url: editor.config.uploadImgUrl,
				type: 'POST',
				data: formData,
				async: true,
				cache: false,
				contentType: false,
				processData: false,
				success: function(returndata) {
					document.getElementById("up_img").style.background = "url(" + returndata + ")  no-repeat ";
					document.getElementById("up_img").style.backgroundSize = "contain"
					document.getElementById("img_url").value = returndata;
					notice.remove();
				},
				error: function(returndata) {
					msg('上传失败', 'info')
				}
			});

		}

		function getData() {

			var tag = "";
			var inform = $("input[name='vicpic']:checked").val();

			var title = document.getElementById('text_tit').value; // 标题
			var news_type = document.getElementById('text_bq').value; // 新闻类型
			var summary = document.getElementById('text_zy').value; // 摘要
			//	     	var	index_pic = document.getElementById('head_upload').value;		// 头像地址
			var index_pic = document.getElementById("img_url").value;
			var content_type = document.getElementById('text_con_type').value; // 内容类型
			var content = document.getElementById('div1').value;
			var child = document.getElementById('taglist').children; // 标签
			var carousel = document.getElementById('carousel').checked ? 1 : 0; // 轮播

			for(var i = 0; i < child.length; i++) {
				if(i != child.length) {
					tag += child[0].innerText + ","
				} else {
					tag += child[0].innerText;
				}

			}

			// 	     	alert(tag)
			// 内容

			// 获取编辑器区域完整html代码
			var html = editor.$txt.html();
			var formatText = editor.$txt.formatText();

			if(inform == undefined) {
				alert('请选择上传文件的类型!');
			} else if(inform == "视频") {
				//alert('选中的是:' + inform);
				news_type = inform;

			} else if(inform == "图文") {
				//alert('选中的是:' + inform)
				news_type = inform;

			}

			if(title == "" || title.length == 0) {
				msg('请输入标题...', 'info')
				return;
			} else if(summary == "" || summary.length == 0) {
				msg('请输入摘要...', 'info')
				return;
			} else if(index_pic == "" || index_pic.length == 0) {
				msg('请选择封面图片...', 'info')
				return;
			} else if(tag == "" || tag.length == 0) {
				msg('请添加文章标签...', 'info')
				return;
			} else if(content_type == "" || content_type.length == 0) {
				msg('请输入内容类型...', 'info')
				return;
			} else if(formatText == "" || formatText.length == 0) {
				msg('请输入内容...', 'info')
				return;
			}
			var param = {
				"action": 4,
				"title": title,
				"content": html,
				"content_type": content_type,
				"news_type": news_type,
				"summary": summary,
				"tag": tag,
				"index_pic": index_pic,
				"carousel": carousel,
			}
			$.ajax({
				//提交数据的类型 POST GET
				type: "POST",
				//提交的网址
				url: getContextPath() + '/news',
				//提交的数据
				data: param,
				//返回数据的格式
				datatype: "json", //"xml", "html", "script", "json", "jsonp", "text".
				//在请求之前调用的函数
				beforeSend: function() {
					//$("#msg").html("logining");

				},
				//成功返回之后调用的函数             
				success: function(data) {
					msg('成功', 'succes')
					window.location.href = 'list.html'
				},
				//调用执行后调用的函数
				complete: function(XMLHttpRequest, textStatus) {

					//HideLoading();
				},
				//调用出错执行的函数
				error: function(XMLHttpRequest, textStatus) {
					//请求出错处理
					// alert(textStatus);
					//   alert(XMLHttpRequest.responseText);
					//   alert(textStatus);
					msg('请求出错', 'error')
				}
			});

		}

		function msg(str, tp) {

			var notice = new PNotify({
				title: '提示信息',
				text: str,
				type: tp,
				buttons: {
					closer: false,
					sticker: false
				}
			});
			setTimeout(function() {
				notice.remove();
			}, 1000)
		}
	</script>

</html>